package cn.supermaster.management.web.service.task.impl;

import cn.supermaster.management.common.page.TableDataInfo;
import cn.supermaster.management.common.response.WebResponse;
import cn.supermaster.management.common.utils.CheckUtils;
import cn.supermaster.management.common.utils.WebHelper;
import cn.supermaster.management.common.utils.poi.ExcelUtil;
import cn.supermaster.management.schedule.domain.ScheduleTask;
import cn.supermaster.management.schedule.domain.ScheduleTaskLog;
import cn.supermaster.management.schedule.service.IScheduleTaskLogService;
import cn.supermaster.management.schedule.service.IScheduleTaskService;
import cn.supermaster.management.web.service.task.ISTaskLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.ModelMap;

import java.util.List;

/**
 * All rights Reserved, Designed By www.super-bytecode.com
 *
 * @ProjectName: supermaster-management
 * @Package: cn.supermaster.management.web.service.task.impl
 * @ClassName: STaskLogServiceImpl
 * @Description: []
 * <p> </p>
 * @Author: [Lance Ting]
 * @Date: 2021/2/19 17:37
 * @Version: V1.0
 * @Copyright: 2021 www.super-bytecode.com Inc. All rights reserved.
 * TODO: 注意,本文件Lance Ting所作,如果转载或使用请标明具体出处!
 **/
@Service
public class STaskLogServiceImpl implements ISTaskLogService {

    private String prefix = "monitor/job";

    @Autowired
    private IScheduleTaskService scheduleTaskService;

    @Autowired
    private IScheduleTaskLogService scheduleTaskLogService;

    /*
     * ================================================
     *  页面跳转功能实现
     * ================================================
     */

    /**
     * @Description: []
     * <p></P>
     * @Title: skipScheduleTaskLogView
     * @Author: Lance Ting
     * @Date: 2021-02-19
     * @Param: modelMap
     * @Param: jobId
     * @Return: java.lang.String
     * @Throws:
     */
    @Override
    public String skipScheduleTaskLogView(ModelMap modelMap, Long jobId) {
        if (CheckUtils.isNotNull(jobId)) {
            ScheduleTask job = scheduleTaskService.selectJobById(jobId);
            modelMap.put("job", job);
        }
        return prefix + "/jobLog";
    }

    /**
     * @Description: []
     * <p></P>
     * @Title: skipScheduleTaskLogDetail
     * @Author: Lance Ting
     * @Date: 2021-02-19
     * @Param: modelMap
     * @Param: jobLogId
     * @Return: java.lang.String
     */
    @Override
    public String skipScheduleTaskLogDetail(ModelMap modelMap, Long jobLogId) {
        modelMap.put("name", "jobLog");
        modelMap.put("jobLog", scheduleTaskLogService.selectJobLogById(jobLogId));
        return prefix + "/detail";
    }


    /*
     * ================================================
     *  Rest功能实现
     * ================================================
     */

    /**
     * @Description: []
     * <p></P>
     * @Title: queryScheduleTaskLogList
     * @Author: Lance Ting
     * @Date: 2021-02-19
     * @Param: taskLog
     * @Return: cn.supermaster.management.common.page.TableDataInfo
     * @Throws:
     */
    @Override
    public TableDataInfo queryScheduleTaskLogList(ScheduleTaskLog taskLog) {
        WebHelper.startPage();
        List<ScheduleTaskLog> list = scheduleTaskLogService.selectJobLogList(taskLog);
        return WebHelper.getDataTable(list);
    }

    /**
     * @param taskLog
     * @Description: []
     * <p></P>
     * @Title: doExportScheduleTaskLog
     * @Author: Lance Ting
     * @Date: 2021-02-19
     * @Param: taskLog
     * @Return: cn.supermaster.management.common.response.WebResult
     * @Throws:
     */
    @Override
    public WebResponse doExportScheduleTaskLog(ScheduleTaskLog taskLog) {

        List<ScheduleTaskLog> list = scheduleTaskLogService.selectJobLogList(taskLog);
        ExcelUtil<ScheduleTaskLog> util = new ExcelUtil<ScheduleTaskLog>(ScheduleTaskLog.class);
        return util.exportExcel(list, "调度日志");
    }

    /**
     * @param ids
     * @Description: []
     * <p></P>
     * @Title: deleteScheduleTaskLogs
     * @Author: Lance Ting
     * @Date: 2021-02-19
     * @Param: ids
     * @Return: cn.supermaster.management.common.response.WebResult
     * @Throws:
     */
    @Override
    public WebResponse deleteScheduleTaskLogs(String ids) {
        WebResponse webResponse = WebResponse.success();
        int rows = scheduleTaskLogService.deleteJobLogByIds(ids);
        if (rows < 1) {
            webResponse = WebResponse.failure();
        }
        return webResponse;
    }

    /**
     * @Description: []
     * <p></P>
     * @Title: doCleanScheduleTaskLog
     * @Author: Lance Ting
     * @Date: 2021-02-19
     * @Param:
     * @Return: cn.supermaster.management.common.response.WebResult
     * @Throws:
     */
    @Override
    public WebResponse doCleanScheduleTaskLog() {
        scheduleTaskLogService.cleanJobLog();
        return WebResponse.success();
    }
}